package sdfi.lee.ReaderFromDisk;

import java.io.File;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Session;

import sdfi.lee.JMS.ConnectionFactory;

public class ListFileFormDisk {
	private String root;
	private int count=0;
	private Connection con=null;
	private Session session=null;
	MapMessage mapmessage=null;
	private MessageProducer producer=null;
	{
		con=ConnectionFactory.getCon();
		Session session;
		try {
			session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
			con.start();
			producer=session.createProducer(session.createQueue("mysql"));
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public void startCon()
	{
		try {
			this.con.start();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void stopCon()
	{
		try {
			this.con.stop();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public ListFileFormDisk()
	{
		
	}
	public String getRoot() {
		return root;
	}
	public void setRoot(String root) {
		this.root = root;
	}
	public void sendMsg(String path)
	{
		try
		{
		
		String p=path.substring(path.indexOf(":")+1);
		p=p.replace("\\", "/");
		if(session==null) session=con.createSession(false, Session.AUTO_ACKNOWLEDGE);
		mapmessage=session.createMapMessage();
		mapmessage.setString("1", "file+"+p);
		producer.send(mapmessage);
		System.out.println("file"+count+++":"+path);
		
		}
		catch(Exception ee)
		{
			ee.printStackTrace();
		}
		
	}
	public void ListFileDirectory()
	{
		if(root==null) return ;
		this.ListFile(root);
	}
	public void ListFile(String path)
	{
		File f=new File(path);
		if(!f.exists()) return ;
		if(f.isDirectory())
		{
			File[] fs=f.listFiles();
			if(fs!=null)
			for(File file:fs)
				this.ListFile(file.getAbsolutePath());
		}
		else
		{
			if(!path.contains("config"))
			this.sendMsg(path);
		}
		
	}
	
	public static void main(String[] arg)
	{
		ListFileFormDisk lffs=new ListFileFormDisk();
		lffs.setRoot("z:/");
		lffs.ListFileDirectory();
		lffs.stopCon();
		
	}
}
